<link rel="stylesheet" type="text/css" href="https://admin.chaozhiyu.cn/public/static/ext/layui/css/layui.css" />
<script src="https://admin.chaozhiyu.cn/public/static/js/jquery-3.1.1.js"></script>
<script src="https://admin.chaozhiyu.cn/public/static/ext/layui/layui.js"></script>
<script>
    layui.use(['layer', 'upload', 'element'], function() {});
    window.ns_url = {
        baseUrl: "https://admin.chaozhiyu.cn/",
        route: ['shop', 'Goods', 'set_area'],
    };
</script>
{block name="resources"}
<style>
    .layui-form-item .layui-input-inline.end-time{float: none;}
    .layui-form-item .layui-input-inline {width: unset;}
</style>
{/block}
{block name="main"}
<div class="layui-form ns-form" lay-filter="user" style="padding:0;height: 600px;overflow-y: scroll;">
    <div class="layui-form-item">
        <div class="layui-input-block" style="margin-left: 10px;">
            <table class="layui-table" id="goods" lay-skin="line">
                <colgroup>
                    <col width="5%">
                    <col width="13.57%">
                    <col width="13.57%">
                    <col width="13.57%">
                    <col width="13.57%">
                    <col width="13.57%">
                    <col width="13.57%">
                </colgroup>
                <thead>
                    <tr>
                        <th style="text-align: center;">ID</th>
                        <th style="text-align: center;">省份</th>
                        <th style="text-align: center;">城市</th>
                        <th style="text-align: center;">区县</th>
                        <th style="text-align: center;">报价</th>
                        <th style="text-align: center;">库存</th>
                        <th style="text-align: center;">操作</th>
                    </tr>
                </thead>
                <tbody>
                    {foreach $list as $k => $v}
                        <tr>
                            <td style="text-align: center;">{$v.id|default="无"}</td>
                            <td style="text-align: center;">{$v.province_name|default="无"}</td>
                            <td style="text-align: center;">{$v.city_name|default="无"}</td>
                            <td style="text-align: center;">{$v.district_name|default="无"}</td>
                            <td style="text-align: center;">{$v.price|default=""}</td>
                            <td style="text-align: center;">{$v.stock|default=""}</td>
                            <td style="text-align: center;"><a href='javascript:;' class="layui-btn layui-btn-danger layui-btn-xs" onclick='deleteLocation(this);' data-id='{$v.id}'>删除</a></td>
                        </tr>
                    {/foreach}
                </tbody>
            </table>
            <div class="layui-form-item">
                <div class="layui-input-inline area-select">
                    <select name="province_id" lay-filter="province_id" lay-verify="province_id">
                        <option value="">省份</option>
                    </select>
                </div>
                <div class="layui-input-inline area-select">
                    <select name="city_id"  lay-filter="city_id" lay-verify="city_id">
                        <option value="">城市</option>
                    </select>
                </div>
                <div class="layui-input-inline area-select">
                    <select name="district_id"  lay-filter="district_id" lay-verify="district_id">
                        <option value="">区县</option>
                    </select>
                </div>
                <div class="layui-input-inline">
                    <input type="number" name="price" placeholder="报价" value="" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-input-inline">
                    <input type="number" name="stock" placeholder="库存" value="" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-input-inline">
                    <button class="layui-btn ns-bg-color" onclick="add()">添加</button>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}
{block name="script" }
<script type="text/javascript" src="SHOP_JS/address.js"></script>
<script src="/public/static/js/common.js"></script>
<script src="/app/shop/view/public/js/common.js"></script>
<script>
    layui.use(['form', 'laydate'], function() {
        var form = layui.form;
        form.render();
        //初始化省市区
        getAreaList(0, 1);
    });

    /**
     * 添加供货区域
     */
    function add(){
        var province_name = $("select[name=province_id] option:selected").text()
            ,city_name = $("select[name=city_id] option:selected").text()
            ,district_name = $("select[name=district_id] option:selected").text()
            ,province_id = $("select[name=province_id] option:selected").val()
            ,city_id = $("select[name=city_id] option:selected").val()
            ,district_id = $("select[name=district_id] option:selected").val()
            ,price = $("input[name=price]").val()
            ,stock = $("input[name=stock]").val();

        if (province_id == '') {
            layer.msg("请至少选择省份");
            return false;
        }
        if (price == '') {
            layer.msg("请填写报价");
            return false;
        }
        if (stock == '') {
            layer.msg("请填写库存");
            return false;
        }

        if(city_id == 0) {city_name = '无';}
        if(district_id == 0) {district_name = '无';}

        $.ajax({
            url: ns.url('shop/order/offer_set_location'),
            type: 'POST',
            dataType: 'json',
            data: {sku_id:'{$sku_id}', province_id:province_id, city_id:city_id, district_id:district_id, price:price, stock:stock},
            success: function (res) {
                if (res.code == 0) {
                    html = '';
                    html += "<tr>";
                    html += "<td style='text-align: center;'>" + res.data.id + "</td>";
                    html += "<td style='text-align: center;'>" + province_name + "</td>";
                    html += "<td style='text-align: center;'>" + city_name + "</td>";
                    html += "<td style='text-align: center;'>" + district_name + "</td>";
                    html += "<td style='text-align: center;'>" + price + "</td>";
                    html += "<td style='text-align: center;'>" + stock + "</td>";
                    html += "<td style='text-align: center;'><a href='javascript:;' class='layui-btn layui-btn-danger layui-btn-xs' onclick='deleteLocation(this);' data-id='"+ res.data.id +"'>删除</a></td>";
                    html += "</tr>";
                    $("#goods tbody").prepend(html);
                } else {
                    layer.msg(res.message);
                }
            }
        });
    }

    /**
     * 删除供货区域
     * @param obj
     */
    function deleteLocation(obj) {
        let id = $(obj).data('id');
        layer.confirm('确定删除?', function(index){
            $.ajax({
                url: ns.url('shop/order/offer_delete_location'),
                type: 'POST',
                dataType: 'json',
                data: {id:id},
                success: function (res) {
                    if (res.code == 0) {
                        window.location.reload();
                    } else {
                        layer.msg(res.message);
                    }
                }
            });
            layer.close(index);
        });
    }
</script>
{/block}